package com.ruoyi.web.controller.bbsAdmin;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bbsAdmin.domain.vo.contentVo;
import com.ruoyi.bbsAdmin.mapper.ContentMapper;
import com.ruoyi.bbsAdmin.service.RedisService;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bbsAdmin.domain.Content;

import com.ruoyi.bbsAdmin.service.IContentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.bbsAdmin.domain.vo.treeSearchStructure;

/**
 * 内容帖Controller
 * 
 * @author ruoyi
 * @date 2022-04-01
 */
@RestController
@RequestMapping("/bbsAdmin/content")
public class ContentController extends BaseController
{
    @Autowired
    private IContentService contentService;

    @Resource
    private ContentMapper contentMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询内容帖列表(弃用）
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/list")
    //这里改一个结构
    public TableDataInfo list(Content content)
    {
        startPage();//控制分页
        List<Content> list = contentService.selectContentList(content);
        return getDataTable(list);
    }

    /**
     * 查询内容帖列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/contentVoList")
    //这里改一个结构
    public TableDataInfo list(contentVo ContentVo)
    {
        startPage();//控制分页
        List<contentVo > list = contentService.selectContentVoList(ContentVo);
        list.sort(Comparator.comparing(contentVo::getCreatetime).reversed());
        return getDataTable(list);
    }

    /**
     * 涉及分类的跨表查询
     * @Param specifyContentStructure
     * @return List
    */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/listByTree")
    public TableDataInfo ContentListByTree(treeSearchStructure treeSearchStructure){
        startPage();
        List<Content> list = contentService.selectTreeContentList(treeSearchStructure);
        if(list!=null){
            return getDataTable(list);
        }else{
            return null;
        }
    }

    /**
     * 导出内容帖列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "内容帖", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Content content)
    {
        List<Content> list = contentService.selectContentList(content);
        ExcelUtil<Content> util = new ExcelUtil<Content>(Content.class);
        util.exportExcel(response, list, "内容帖数据");
    }

    /**
     * 获取内容帖详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping(value = "/{contentid}")
    public AjaxResult getInfo(@PathVariable("contentid") String contentid)
    {
        return AjaxResult.success(contentService.selectContentByContentid(contentid));
    }

    /**
     * 新增内容帖
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "内容帖", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Content content)
    {
        return toAjax(contentService.insertContent(content));
    }

    /**
     * 修改内容帖
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "内容帖", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Content content)
    {
        return toAjax(contentService.updateContent(content));
    }

    /**
     * 删除内容帖
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "内容帖", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contentids}")
    public AjaxResult remove(@PathVariable String[] contentids)
    {
        return toAjax(contentService.deleteContentByContentids(contentids));
    }

    @ApiOperation("根据帖子ID删除redis中缓存的某个帖子")
    @GetMapping("/deletePostInRedisById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "帖子ID", required = true),
    })
    public Boolean deletePostInRedisById(String contentId) {
        try {
            Boolean result = contentService.deletePostInRedisById(contentId);
            return result;

        } catch (Exception e) {
            return false;
        }

    }

    @ApiOperation("删除redis中缓存的置顶帖子")
    @GetMapping("/deleteTopPostInRedis")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校ID", required = true),
    })
    public Boolean deleteTopPostInRedisById(int schoolId) {
        try {
            Boolean result = contentService.deleteTopPostInRedis(schoolId);
            return result;

        } catch (Exception e) {
            return false;
        }

    }

    @ApiOperation("删除该学校redis中缓存的所有帖子")
    @GetMapping("/deletePostListInRedis")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校ID", required = true),
    })
    public Boolean deleteHomePostListInRedis(int schoolId) {
        try {
            Boolean result = contentService.deleteHomePostListInRedis(schoolId);
            return result;

        } catch (Exception e) {
            return false;
        }

    }

    @ApiOperation("重置该学校redis中缓存的所有帖子")
    @GetMapping("/updateHomePostListInRedis")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校ID", required = true),
    })
    public Boolean updateHomePostListInRedis(int schoolId) {
        try {
            Boolean result = contentService.updateHomePostListInRedis(schoolId);
            return result;

        } catch (Exception e) {
            return false;
        }

    }

    @ApiOperation("根据帖子ID更新redis中缓存的某个帖子内容，帖子+评论")
    @GetMapping("/updatePostInfoInRedisById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "帖子ID", required = true),
    })
    public int updatePostInfoInRedisById(String contentId) {
        try {
            int result = redisService.updateContentInfoInRedis(contentId);
            return result;

        } catch (Exception e) {
            return 0;
        }

    }

    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/allSchoolContentVoList")
    //这里改一个结构
    public TableDataInfo allSchoolContentVoList(contentVo ContentVo)
    {
        startPage();//控制分页
        List<contentVo > list = contentService.selectSAllSchoolContentVoList(ContentVo);
        list.sort(Comparator.comparing(contentVo::getCreatetime).reversed());
        return getDataTable(list);
    }

    @GetMapping("/getHotPostList")
    //这里改一个结构
    public List<contentVo> getHotPostList(contentVo ContentVo)
    {
        List<contentVo> list = contentService.selectHotPostList(ContentVo);
        return list;
    }

}
